package cn.shengongying.service.impl;

import cn.shengongying.dao.CustomerMapper;
import cn.shengongying.model.Customer;
import cn.shengongying.service.ICustomerService;
import cn.shengongying.service.IUserDiscountInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户/雇主表 服务实现类
 * </p>
 *
 * @author RXK
 * @since 2022-01-05
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {

    private final CustomerMapper customerMapper;

    private final IUserDiscountInfoService userDiscountInfoService;

    public CustomerServiceImpl(CustomerMapper customerMapper, IUserDiscountInfoService userDiscountInfoService) {
        this.customerMapper = customerMapper;
        this.userDiscountInfoService = userDiscountInfoService;
    }

    @Override
    public Customer login(String phone) {
        Customer customer = customerMapper.findByPhone(phone);
        if (null == customer) {
            customer = Customer.build(phone);
            customerMapper.insert(customer);
            userDiscountInfoService.newUser(customer.getId());
        }
        return customer;
    }

    @Override
    public List<Customer> listAndPage(String nickName, String telephone, Integer type, int start, int length) {
        return customerMapper.listAndPage(nickName,telephone,type,start,length);
    }

    @Override
    public int calculateCount(String nickName, String telephone, Integer type) {
        return customerMapper.count(nickName,telephone,type);
    }

    @Override
    public List<Integer> statistic(Integer type) {
        return customerMapper.statistic(type);
    }
}
